package com.kancy.publicapi.web.controller;

import com.kancy.publicapi.common.util.Md5Util;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * Md5Controller
 * </p>
 *
 * @author: kancy
 * @date: 2019/12/5 18:00
 **/
@RestController
public class Md5Controller {

    /**
     * md5
     * @param value1
     * @param value2
     * @param flag
     * @return
     */
    @RequestMapping(value = "/md5/{value}", method = {RequestMethod.GET, RequestMethod.POST})
    public String md5(@PathVariable(value = "value", required = false) String value1,
                      @PathVariable(value = "value", required = false) String value2,
                      @RequestParam("upper") boolean flag){
        String value = null;
        if (StringUtils.isEmpty(value1) && Objects.isNull(value2)){
            return "fail：value is empty";
        }
        if(!StringUtils.isEmpty(value1)){
            value = value1;
        }
        if(!Objects.nonNull(value2)){
            value = value2;
        }
        String md5 = Md5Util.getMD5(value);
        return flag ? md5.toUpperCase() : md5;
    }
}
